/**
 * 走访-入住户数据
 * Created by xc on 2015/6/18.
 */
define(function (require, exports, module) {
  'use strict';
  module.exports = function (app) {
    app.register.controller('OccupancyCtrl', OccupancyCtrl);
    OccupancyCtrl.$inject = ['$scope', '$filter', 'MHService'];
    function OccupancyCtrl($scope, $filter, MHService) {
      $scope.$watch('roomno', function () {
        //房间号有变化
        if ($scope.roomno) {
          MHService.postReq({url: MHService.URL.validate_occupancy + '?roomno=' + $scope.roomno},
            function (data) {
              if (!data.data) {
                $scope.isShow = !data.data;
              } else {
                MHService.Toast('当前房号已有数据');
              }
            });
        }
      }, true);
      $scope.addOccupacy = function (_data) {
        var req = angular.copy(_data);
        req.actualtime = $filter('date')(_data.actualtime, 'yyyy-MM-dd');
        req.endtime = $filter('date')(_data.endrentime, 'yyyy-MM-dd');
        req.plantime = $filter('date')(_data.plantime, 'yyyy-MM-dd');
        req.starttime = $filter('date')(_data.startrentime, 'yyyy-MM-dd');
        req.roomno = $scope.roomno;
        console.log(req);
        MHService.postReq({url: MHService.URL.add_occupancy, _req: req},
          function (result) {
            console.log(result);
            if (result.success) {
              MHService.Toast('提交成功');
              history.go(-1);
            } else {
              MHService.Toast('提交失败，请重试');
            }
          });
      }
    }
  };
});
